/**
 * helper
 */

import _ from 'lodash';

/**
 * 加载Api下的es子模块
 * @returns
 */
export function requireApiModules() {
  // path 也不能是变量
  // filter 只能是 /xx/，不能是字符串或者Reg
  //否则报错：  __webpack_require__(...).context is not a function
  const files = require.context('@/api', true, /modules\/(\w+\/){0,}\w+\/*.js$/);
  const fileNames = files.keys();
  let result = {};
  fileNames.forEach(path => {
    const sp = path.replace(/\.js$/, '').split('/');
    const deep = sp.splice(1, sp.length - 1);
    _.set(result, deep.join('.'), files(path).default);
  });

  return result.modules;
}
